package com.bjyhl.medical.main.news.cat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjyhl.ui.recycler.DataConverter;
import com.bjyhl.ui.recycler.ItemType;
import com.bjyhl.ui.recycler.MultipleFields;
import com.bjyhl.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * 新闻栏目列表
 */
public class NewsCatListDataConverter extends DataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");

        final int size = dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);

            final Integer id = data.getInteger("id");
            final String title = data.getString("cat_name");

            int type = ItemType.NEWS_CAT; //

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.ID, id)
                    .setField(MultipleFields.TITLE, title)
                    .setField(MultipleFields.TAG, false)
                    .build();
            ENTITIES.add(entity);
        }

        ENTITIES.get(0).setField(MultipleFields.TAG, true);

        return ENTITIES;
    }
}
